const config = require('config.js')
App({
  onLaunch: function () {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (detail) {
              wx.login({
                success(res) {
                  console.log(res.code);
                  if (res.code) {
                    //发起网络请求
                    wx.request({
                      url: config.requestUrl +'/login',
                      method: 'post',
                      data: {
                        code: res.code,
                        nickname: detail.userInfo.nickName,
                        headimgurl: detail.userInfo.avatarUrl,
                        sex: detail.userInfo.gender
                      }, success(res) {
                        getApp().globalData.userInfo = res.data.data;
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          });

        }else{
          wx.reLaunch({
            url: '/pages/authorization/authorization',
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null
  }
})